#!/bin/bash
# H0 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# H0 X
# H0 X   libAtoms+QUIP: atomistic simulation library
# H0 X
# H0 X   Portions of this code were written by
# H0 X     Albert Bartok-Partay, Silvia Cereda, Gabor Csanyi, James Kermode,
# H0 X     Ivan Solt, Wojciech Szlachta, Csilla Varnai, Steven Winfield.
# H0 X
# H0 X   Copyright 2006-2010.
# H0 X
# H0 X   These portions of the source code are released under the GNU General
# H0 X   Public License, version 2, http://www.gnu.org/copyleft/gpl.html
# H0 X
# H0 X   If you would like to license the source code under different terms,
# H0 X   please contact Gabor Csanyi, gabor@csanyi.net
# H0 X
# H0 X   Portions of this code were written by Noam Bernstein as part of
# H0 X   his employment for the U.S. Government, and are not subject
# H0 X   to copyright in the USA.
# H0 X
# H0 X
# H0 X   When using this software, please cite the following reference:
# H0 X
# H0 X   http://www.libatoms.org
# H0 X
# H0 X  Additional contributions by
# H0 X    Alessio Comisso, Chiara Gattinoni, and Gianpietro Moras
# H0 X
# H0 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

# Convert a netcdf file to a movie

if [[ $# == 0 ]]; then
    echo "Usage: $0 <AtomEye view script> <movie file> <netcdf file> <Extra command file>"
    exit 1
fi

view=$1
movie=$2
nc=$3
extras=$4

echo "View script: $view"
echo "Movie output file: $movie"
echo "NetCDF file: $nc"
echo "Extra command file: $extras"

echo Removing old Jpg dir...
rm -rf Jpg

#echo Generating comment file...
#for i in $all_cfgs
#do 
#  echo -n $i Jpg/`basename $i .cfg`.jpg
#  head -2 $i | tail -1 | cut -c2\-
#done > $index

echo Generating AtomEye script
n=0
script_n=1

cp $view scr_movie_${script_n}
if [[ $extras != "" ]] ; then
  cat $extras >> scr_movie_${script_n}
fi

nframe=$(ncstat -c $nc | awk '{print $1}')

echo "Found $nframe frames"

base=`basename $nc .nc`
ndigit=$(python -c 'from math import *; print int(ceil(log10('$nframe')))')

for i in $(seq 0 $((nframe-1)))
#echo "Processing Frame $i"
do
  if [[ -e `basename $i .cfg`.clr ]]
      then
      echo load_atom_color `printf ${base}%0${ndigit}d $i`.clr  >> scr_movie_${script_n}
  fi
  echo capture_jpg Jpg/`printf ${base}%0${ndigit}d $i`.jpg  >> scr_movie_${script_n}
  echo load_config_forward >> scr_movie_${script_n}
  n=$((n+1))
  if ((n >= 1000)); then 
      script_n=$((script_n+1))
      n=0

      cp $view scr_movie_${script_n}
      echo shift_xtal 0 0 >> scr_movie_${script_n}
      if [[ $extras != "" ]] ; then
        cat $extras >> scr_movie_${script_n}
      fi
  fi

done

for i in $(seq 1 $script_n); do
    echo quit >> scr_movie_$i
done

echo Running AtomEye...

for i in $(seq 1 $script_n); do
    A -nofep -nowindow -f=scr_movie_$i $nc # > /dev/null 2>&1
done

#echo Captioning frames and adding graph...
#graph_caption $index 0 400 0 15.0 Temp x1y1 lines G x1y2 lines


echo Making movie...

## MENCODER

#mencoder "mf://Jpg/*.jpg" -mf fps=15 -o $movie -ovc lavc -lavcopts vcodec=msmpeg4:vhq:vbitrate=15800

## FFMPEG

# Get basename of cfg files

ffmpeg -i Jpg/${base}%0${ndigit}d.jpg -r 25 -b 30M $movie
